package com.xxxx.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxxx.server.mapper.AdminMapper;
import com.xxxx.server.mapper.LectureinfoMapper;
import com.xxxx.server.mapper.LecturereverseMapper;
import com.xxxx.server.pojo.Lectureinfo;
import com.xxxx.server.pojo.Lecturereverse;
import com.xxxx.server.pojo.RespBean;
import com.xxxx.server.service.ILecturereverseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LT
 * @since 2022-02-27
 */
@Service
public class LecturereverseServiceImpl extends ServiceImpl<LecturereverseMapper, Lecturereverse> implements ILecturereverseService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private LecturereverseMapper lecturereverseMapper;

   @Autowired
   private LectureinfoMapper lectureinfoMapper;

    @Override
    public List<Lecturereverse> getALlLectureReverseByUserId(Lecturereverse lecturereverse) {


        return  lecturereverseMapper.getALlLectureReverseByUserId(lecturereverse);

    }
    /**
     * 添加传入讲座id进行讲座预定
     * @param lecturereverse
     * @return
     */
    @Override
    public RespBean addLectureReverse(Lecturereverse lecturereverse) {
//LectureinfoMapper中添加查询方法， 然后从lecturereverse中获取讲座id 通过查询方法查出剩余讲座数量
        //设置预定时间为当前时间
        lecturereverse.setReverseDate(LocalDateTime.now());
        Integer remainNumbers=lectureinfoMapper.getRemainNumbers(lecturereverse.getLectureId());

       List<Lectureinfo> lecture =lectureinfoMapper.getLectureInfoByid(lecturereverse.getLectureId());
       if(lecture.size()==0)
       {
           return RespBean.error("讲座不存在,请重新输入!");
       }
        {
            if(remainNumbers>0)
            {
                //通过讲座id和用户id 查询
                Integer lectureId=lecturereverseMapper
                        .selectIsExitLectureIdByUserId
                                ( lecturereverse.getLectureId(), lecturereverse.getUserId());

                if(lectureId!=null)
                {
                    return RespBean.error("预定失败,用户已经预定过该讲座");
                }
                else
                {
                    //插入预定信息
                    lecturereverseMapper.addLectureReverse(lecturereverse);
                    //更新讲座座位数量
                    lectureinfoMapper.updateByIdNumbers(lecturereverse.getLectureId(),remainNumbers-1);
                    return RespBean.success("预定成功!");
                }

            }
            else
            {
                return RespBean.success("预定失败,讲座数量可能不足!");
            }
        }


    }

    /**
     * 删除预定信息,还需要修改剩余座位数量
     * @param lecturereverse
     * @return
     */
    @Override
    public RespBean deleteLectureReverseById(Lecturereverse lecturereverse) {

        //1.设置讲座表的剩余座位,然后更新

        Integer numbers=lectureinfoMapper.getRemainNumbers(lecturereverse.getLectureId())+1;

        //减少讲座座位数量
        lectureinfoMapper.updateByIdNumbers(lecturereverse.getLectureId(),numbers);

        //2.删除座位
        Integer isDeleted=lecturereverseMapper.deleteById(lecturereverse.getId());
       if(isDeleted==1)
        {
            return RespBean.success("删除成功!");
        }
       else
       {
           //更新失败以后 恢复修改的讲座座位数量
           numbers=numbers-1;
           lectureinfoMapper.updateByIdNumbers(lecturereverse.getLectureId(),numbers);
           return RespBean.error("删除失败!");
       }
    }


}
